﻿using AutoMapper;
using MediatR;
using WarehouseManagement.Application.Command.WarehouseTypeCommand;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;
namespace WarehouseManagement.Application.CommandHandler.WareTypeCommandHandler
{
    public class CreateWareTypeCommandHandler : IRequestHandler<CreateWareTypeCommand, int>
    {

        private readonly IWareTypeRepository _waretype;

        protected readonly IMapper _mapper;

        public CreateWareTypeCommandHandler(IWareTypeRepository waretype, IMapper mapper)
        {
            this._waretype = waretype;
            this._mapper = mapper;
        }



        public async Task<int> Handle(CreateWareTypeCommand request, CancellationToken cancellationToken)
        {
            var result = (await _waretype.GetAll()).Where(x => x.WCTypeName == request.AddWareTypeDTO.WCTypeName).ToList();
            if(result.Count > 0)
            {
                return -1;//已存在该类型
            }
            else
            {
                return await _waretype.AddAsync(_mapper.Map<WarehouseCategory>(request.AddWareTypeDTO));
            }
        }





    }
}
